/**
* Represents a ToolBox that can receive a group of other UI elements.
* @author Default
* @version 0.1
*/
import com.thelab.ui.*;

dynamic class com.thelab.ui.ToolBox extends UIElement
{	
	var _title:Label;
	var __boxWidth:Number;
	var __boxHeight:Number;
	var __titlePadding:Number;
	var _margin:Number;
	var _stage:MovieClip;
	var _mask:MovieClip;
	var _resizer:HitArea;
	var _drag:HitArea;
	var _child:Array;
	
	public function ToolBox(scope:MovieClip,px:Number,py:Number,label:String,width:Number,height:Number)
	{
		super("UIToolBox",scope);
		_mask = target.createEmptyMovieClip("_mask",target.getNextHighestDepth());
		_stage = target.createEmptyMovieClip("_stage",target.getNextHighestDepth());
		_stage.setMask(_mask);
		target._x = px || 0;
		target._y = py || 0;		
		
		style.backalpha = 100;
		style.backcolor = 0xe5e5e5;
		style.linethick = 2.0;
		style.linecolor = 0x000000;
		style.linealpha = 10;
		__titlePadding = 9;
		_margin = 20;
		_title = new Label(target,label || "",3,0);
		var tw:Number = _title._tWidth+__titlePadding+20;
		var th:Number = _title._tHeight;
		_title._y-= th+1;
		width  = width  || 50;
		height = height || 50;
		__boxWidth = (width<tw)? tw : width;
		__boxHeight = height;
		
		_resizer = new HitArea(scope,this,px+__boxWidth,py+__boxHeight,10,10,-5,-5);
		_resizer._fixed = false;
		_resizer.onDrag = dragResize;
		
		_drag = new HitArea(scope,this,target._x,target._y,tw,_title._tHeight,0,-_title._tHeight);
		_drag._fixed = false;
		_drag.onDrag = dragMove;
		
		clearEvents();
		draw();
	}
	
	function addUI(ui_name:String)
	{
		var a:Array =arguments;
		var i:Number = 1;
		var ref  = UIElement.getUI(ui_name,_stage,a[i++],a[i++],a[i++],a[i++],a[i++],a[i++]);				
		return ref;
		
	}	

	function dragResize(hit:HitArea)
	{
		
		var tb:ToolBox = hit._ui;
		var dx:Number = (hit._x - tb._x);
		var dy:Number = (hit._y - tb._y);
		tb._boxWidth = dx;
		tb._boxHeight = dy;
		
	}
	function dragMove(hit:HitArea)
	{		
		var tb:ToolBox = hit._ui;	
		tb._x = hit._x;
		tb._y = hit._y;
		tb._resizer._x = tb._x+tb._boxWidth;
		tb._resizer._y = tb._y+tb._boxHeight;
	}
	
	function get _titleHeight():Number { return _title._tHeight; }
	function get _titleWidth():Number  { return _title._tWidth+__titlePadding; }
	
	function get _boxHeight():Number { return __boxHeight; draw();}
	function set _boxHeight(v:Number)
	{ 
		__boxHeight = v; 
		//__boxHeight = (__boxHeight<_titleHeight)? _title._tHeight+60:__boxHeight		
		draw(); 
	}
	function get _boxWidth():Number { return __boxWidth;draw(); }
	function set _boxWidth(v:Number)
	{ 
		__boxWidth = v; 
		//__boxWidth = (__boxWidth<_titleWidth)? _titleWidth+50 : __boxWidth;		
		draw(); 
	}
	
	private function update_style()
	{
		//trace("UIDot> Setting Style: ");
		//trace(style);
		draw();
	}	
	function draw()
	{		
		var tw:Number = _title._tWidth+__titlePadding;
		var th:Number = _title._tHeight;
		
		_mask.clear();
		_mask.beginFill(0x0,20);
		_mask.lineTo(__boxWidth,0);
		_mask.lineTo(__boxWidth,__boxHeight);
		_mask.lineTo(0,__boxHeight);
		_mask.lineTo(0,0);
		_mask.endFill();
		
		target.clear();
		target.lineStyle(style.linethick,style.linecolor,style.linealpha);
		target.beginFill(style.backcolor,style.backalpha);
		
		toolbox(0,0,__boxWidth,__boxHeight,_titleWidth,_titleHeight);
		target.endFill();
	}
	
}